package com.example.littleken.sqlitetext;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class ThridActivity extends AppCompatActivity {
    private  EditText et_name,et_age,et_id;
    private RadioGroup genderGp;
    private String genderStr = "男";//用于保存用户输入的性别
    SQLiteDatabase db;
    private ListView stuList;
    private SQLiteDao dao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thrid);
        dao = new SQLiteDao(this);
        initView();
    }

    private void initView() {
        et_name = (EditText) findViewById(R.id.name_edt);
        et_age = (EditText) findViewById(R.id.age_edt);
        et_id = (EditText) findViewById(R.id.id_edt);
        genderGp = (RadioGroup) findViewById(R.id.gender_gp);
        stuList = (ListView) findViewById(R.id.stu_list);
        genderGp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if(checkedId == R.id.male){
                    genderStr = "男";
                }else{
                    genderStr = "女";
                }
            }
        });
    }

    public void operate(View v){
        String idStr = et_id.getText().toString();
        String nameStr = et_name.getText().toString();
        String ageStr = et_age.getText().toString();

        switch (v.getId()){
            case R.id.insert_btn:
                Toast.makeText(ThridActivity.this,"添加",Toast.LENGTH_SHORT).show();
                Student stu;
                try{
                    stu = new Student(nameStr,Integer.parseInt(ageStr),genderStr);
                }catch (NumberFormatException e){
                    stu = new Student(nameStr,0,genderStr);
                }
                dao.insert(stu);
                break;
            case R.id.select_btn:
                Toast.makeText(ThridActivity.this,"查询",Toast.LENGTH_SHORT).show();
                String key = "";
                String value = "";
                if(!nameStr.equals("")){
                    key = "name";
                    value = nameStr;
                }else if(!ageStr.equals("")){
                    key = "age";
                    value = ageStr;
                }else if(!idStr.equals("")){
                    key = "_id";
                    value = idStr;
                }
                Cursor c;
                if(key.equals("")){
                    c= dao.select();
                }else {
                    c= dao.select(key,value);
                }
                Log.e("TAG",c.toString());
                stuList.setAdapter(setAdapter(this,c));
                break;
            case R.id.delete_btn:
                Toast.makeText(ThridActivity.this,"删除",Toast.LENGTH_SHORT).show();
                String[] params = getparams(nameStr,ageStr,idStr);
                dao.delete(params[0],params[1]);
                Cursor cc = dao.select();
                stuList.setAdapter(setAdapter(this,cc));
                break;
            case R.id.update_btn:
                Toast.makeText(ThridActivity.this,"修改",Toast.LENGTH_SHORT).show();
                Student stusecond;
                if(idStr.equals("")){
                    Toast.makeText(ThridActivity.this,"id不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    try{
                        stusecond = new Student(Integer.parseInt(idStr),nameStr,Integer.parseInt(ageStr),genderStr);
                    }catch (NumberFormatException e){
                        stusecond = new Student(Integer.parseInt(idStr),nameStr,0,genderStr);
                    }
                    dao.update(stusecond);
                }
                Cursor ccc = dao.select();
                stuList.setAdapter(setAdapter(this,ccc));
                break;
        }
        et_id.setText("");
        et_name.setText("");
        et_age.setText("");
    }
  private SimpleCursorAdapter setAdapter(Context context,Cursor c){
      SimpleCursorAdapter adapter = new SimpleCursorAdapter(ThridActivity.this,R.layout.item,c,new String[]{
              "_id","name","age","gender"},new int[]{
              R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item
      }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
      return adapter;
  }

  private String[] getparams(String nameStr,String ageStr,String idStr){
      String[] params = new String[2];
      if(!nameStr.equals("")){
          params[0] = "name";
          params[1] = nameStr;
      }else if(!ageStr.equals("")){
          params[0] = "age";
          params[1] = ageStr;
      }else if(!idStr.equals("")){
          params[0] = "_id";
          params[1] = idStr;
      }
      return params;
  }
}
